package com.newear.marathon.utils;

import java.io.BufferedInputStream;
import java.security.DigestInputStream;
import java.security.MessageDigest;

public class Md5Util {
    private static MessageDigest md5 = null;

    static {
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    /**
     * 用于获取一个String的md5值
     *
     * @param str
     *
     * @return
     */
    public synchronized static String getMd5(String str)
    {
        byte[] bs = md5.digest(str.getBytes());
        StringBuilder sb = new StringBuilder(40);
        for (byte x : bs) {
            if ((x & 0xff) >> 4 == 0) {
                sb.append("0").append(Integer.toHexString(x & 0xff));
            } else {
                sb.append(Integer.toHexString(x & 0xff));
            }
        }
        return sb.toString();
    }

    /**
     * 用于获取一个byte[]的md5值
     *
     * @param content 需要计算byte[]
     *
     * @return md5
     */
    public synchronized static String getMd5(byte[] content)
    {
        byte[] bs = md5.digest(content);
        StringBuilder sb = new StringBuilder(40);
        for (byte x : bs) {
            if ((x & 0xff) >> 4 == 0) {
                sb.append("0").append(Integer.toHexString(x & 0xff));
            } else {
                sb.append(Integer.toHexString(x & 0xff));
            }
        }
        return sb.toString();
    }

    public static String digest(byte[] content)
    {
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            byte[] bs = messageDigest.digest(content);
            StringBuilder sb = new StringBuilder();
            for (byte x : bs) {
                if ((x & 0xff) >> 4 == 0) {
                    sb.append("0").append(Integer.toHexString(x & 0xff));
                } else {
                    sb.append(Integer.toHexString(x & 0xff));
                }
            }
            return sb.toString();
        } catch (Exception e) {
            return null;
        }
    }

    public static String digest(String value)
    {
        try {
            return Md5Util.digest(value.getBytes("UTF-8"));
        } catch (Exception e) {
            return null;
        }
    }

    public static String digest(BufferedInputStream inputStream)
    {
        try {
            byte[] buffer = new byte[8192];
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            DigestInputStream dis = new DigestInputStream(inputStream, messageDigest);
            while (dis.read(buffer) != -1) ;
            dis.close();
            byte[] bs = messageDigest.digest();
            StringBuilder sb = new StringBuilder();
            for (byte x : bs) {
                if ((x & 0xff) >> 4 == 0) {
                    sb.append("0").append(Integer.toHexString(x & 0xff));
                } else {
                    sb.append(Integer.toHexString(x & 0xff));
                }
            }
            return sb.toString();
        } catch (Exception e) {
            return null;
        }
    }

    public static void main(String args[])
    {
        String testStr = "jdbc:oracle://then231";
        System.out.println(Md5Util.digest(testStr.trim()));
    }
}
